package com.example.spider.provider.speedTest;

import cn.hutool.core.util.ReUtil;
import com.example.spider.starter.SpiderStarter;
import com.example.spider.task.AbstractSpiderTask;
import com.example.spider.task.SpiderResultHandler;
import com.example.spider.task.SpiderTaskQueue;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 测试目标网页所有子链接访问速度
 * 一般用于请求导航页面
 *
 * @author anonymous
 */
@Slf4j
@Component
public class SpeedTestListHandler implements SpiderResultHandler<SpeedTestListHandler.SpeedTestTask, String>, SpiderStarter {

    public static void test(String url) {
        SpiderTaskQueue.putTask(
                new SpeedTestTask()
                        .setUrl(url));
    }

    /**
     *
     */
    @Override
    public void startSpiders() {
    }

    /**
     * 分析列表结果，创建下载详情任务
     */
    @Override
    public void handle(SpeedTestTask task, String result) {

        log.debug("prepare handing {} {} ", task.getClass().getSimpleName(), task.getUrl());

        List<String> urls = ReUtil.findAllGroup1("<a href=\"(.*?)\".*? title=\"(.*?)\"", result);
        List<String> titles = ReUtil.findAll("<a href=\"(.*?)\".*? title=\"(.*?)\"", result, 2);
        if (CollectionUtils.isEmpty(urls)) {
            log.warn("illegal List result " + task.getUrl());
            return;
        }

        for (int i = 0; i < titles.size(); i++) {
            String title = titles.get(i);
            title = title.replace(" ", "").replace("\n", "").trim();
            String url = urls.get(i);
            if (!url.startsWith("http")) {
                continue;
            }
            SpeedTestDetailHandler.SpeedTestDetailTask detailTask = new SpeedTestDetailHandler.SpeedTestDetailTask()
                    .setTitle(title)
                    .setUrl(url);

            // 创建爬取详情的任务
            SpiderTaskQueue.putTask(detailTask);
        }

        log.info("finished handle {} {} ", task.getClass().getSimpleName(), task.getUrl());
    }

    @Getter
    @Setter
    @Accessors(chain = true)
    public static class SpeedTestTask extends AbstractSpiderTask<String> {


    }
}